﻿<html>
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body>
<div class="demoTable">
    姓名:
    <div class="layui-inline">
        <input class="layui-input" name="username" id="username" placeholder="姓名" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="demo" lay-filter="test"></table>
<!-- 自定义的控件 -->
<!--<div id="toolbar" style="display: none; ">-->
<!--    <div class="layui-btn-group">-->
<!--        <button class="layui-btn layui-btn-sm" lay-event="add" data-method="notice" data-type="auto">添加</button>-->
<!--    </div>-->
<!--</div>-->
<script type="text/jsp" id = "barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="poss">通过</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="noposs">不通过</a>
</script>
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    layui.use(['table','layer','jquery'], function(){
        var table = layui.table;
        var  layer = layui.layer;
        var  $ = layui.jquery;
        var form = layui.form;
        table.render({
            elem: '#demo'
            ,height: document.body.clientHeight-180
            ,url: '/userRole/queryAll' //数据接口
            ,page:true
            ,id:'testReload'
            ,cols: [[ //表头
                 {field: 'id', title: '序号',  sort: true}
                , {
                    field: 'user', title: '昵称',
                    templet: function (d) {
                        return d.user.username;
                    }
                }
                , {
                    field: 'user', title: '邮箱',
                    templet: function (d) {
                        return d.user.email;
                    }
                }
                , {
                    field: 'role', title: '注册角色',
                    templet: function (d) {
                        return d.role.description;
                    }
                }
                ,{field: 'create_datetime', title: '注册时间',
                    templet: "<div>{{layui.util.toDateString(d.create_datetime, 'yyyy-MM-dd')}}</div>"}
                ,{field: 'right', title: '操作', toolbar:'#barDemo'}
            ]]
            ,toolbar:'#toolbar',//表格头信息
            parseData: function (rs) {//数据格式解析
                console.log(rs);
                if (rs.code == 200) {
                    return {
                        "code": rs.code,
                        "msg": rs.msg,
                        "count": rs.date.total,
                        "data": rs.date.list
                    }
                }
            },
            response: {	// 设置响应码
                statusCode: 200 //规定成功的状态码，默认：0
            }
        });

        //监听行事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            console.log(data)
            if(obj.event === 'poss'){
                layer.confirm('确定通过吗?', function (index) {
                        $.ajax({
                            type: "post",
                            url: '/userRole/pass',
                            data: {id: data.id},
                            dataType: "json",
                            success: function (da) {
                                if (da.code == 200) {
                                    layer.msg(da.msg)
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    // location.reload();//刷新当前页面
                                }else {
                                    layer.msg(da.msg)
                                }
                            },
                            error: function (da) {
                                alert("出现错误");
                                return false;
                            }
                        }) //ajax结束
                        layer.close(index);
                    });
                }
            else if(obj.event === 'noposs') {
                    layer.confirm('确定不通过吗?',function (index) {
                        $.ajax({
                            type: "post",
                            url: '/userRole/nopass',
                            data: {id: data.id},
                            dataType: "json",
                            success: function (da) {
                                if (da.code == 200) {
                                    layer.msg(da.msg)
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    // location.reload();//刷新当前页面
                                }else {
                                    layer.msg(da.msg)
                                }
                            },
                            error: function (da) {
                                alert("出现错误");
                                return false;
                            }
                        }) //ajax结束
                        layer.close(index);
                    });
                }

        });
        //"用户添加页面",
        function open(title,url){
            layer.open({
                type: 2
                ,title: title
                ,content:url
                ,offset: '50px'
                ,area: ['500px', '60%']
                ,end:function () {
                    table.reload('testReload');
                }
            });
        }

        function searchBtn(){

        }

        var $ = layui.$, active = {
            reload: function () {
                var username = $('#username');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "username": username.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>